package com.tfy.liquor.dao;

import com.tfy.liquor.bean.ProductOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 产品订单信息
 */
@Mapper
@Service
public interface ProductOrderDao {
    // 创建订单信息
    @Insert("insert into product_order values(" +
            "'${id}', " +
            "${userId}, " +
            "'${parentOrderId}', " +
            "'${orderId}', " +
            "'${productId}', " +
            "'${productEncode}', " +
            "'${productName}', " +
            "${purchasePrice}, " +
            "${salesPrice}, " +
            "0, " +
            "${number}, " +
            "null, " +
            "1, " +
            "'${addressId}', " +
            "'${remark}', " +
            "'${createTime}', " +
            "null, " +
            "null, " +
            "null)"
    )
    int createProductOrder(ProductOrder productOrder);

    // 查询订单信息
    @Select("<script>" +
            "select * from product_order " +
            "where 1=1" +
            "<if test='userId != null'>and userId = ${userId}</if>" +
            "<if test='parentOrderId != null'>and parentOrderId = '${parentOrderId}'</if>" +
            "<if test='productName != null'>and productName like '%${productName}%'</if>" +
            "<if test='status != null'>and status = ${status}</if>" +
            "<if test='addressId != null'>and addressId = ${addressId}</if>" +
            "<if test='createTime != null'>and createTime like '%${createTime}%'</if>" +
            "order by id desc" +
            "</script>"
    )
    ArrayList<ProductOrder> getProductOrder(ProductOrder productOrder);

    // 根据父订单编号获取订单付款金额
    @Select("select sum(salesPrice*number) from product_order where userId=${userId} and parentOrderId='${parentOrderId}'")
    String getOrderAmountByParentOrderId(Long userId, String parentOrderId);

    // 订单付款
    @Update("update product_order set status=2, actualPayment=${actualPayment}, payTime='${payTime}' where id='${id}'")
    int payProductOrder(ProductOrder productOrder);
}
